# -*- coding:utf-8 -*-
"""Created By Corvo"""


class TipMgr(object):
    """提示管理"""
    __mVersion__ = 1

    def __init__(self):
        self.node_storage = {}  # 结点存储

    # 注册结点
    def RegisterNode(self, node, offset=(0, 0), scale=1.0):
        # type: (TipNode, tuple, float) -> bool
        """注册结点"""

    # 反注册结点
    def UnRegisterNode(self, node):
        # type: (TipNode) -> bool
        """反注册结点"""

    # 更新结点数据
    def UpdateNode(self, node_id, state):
        # type: (str, int) -> bool
        """更新结点数据"""
        if node_id not in self.node_storage:
            return False
        node = self.node_storage[node_id]  # type: TipNode
        return node.SetState(state)

    # 构建结点树
    def BuildTree(self, tree):
        # type: (dict) -> bool
        """构建结点树"""
        bind_cache = {}
        for node_id, config in tree.items():
            assert isinstance(config, dict)
            node = TipNode(node_id)
            self.node_storage[node_id] = node
            parent_id = config.get("parent")
            if not parent_id:
                continue
            if parent_id not in self.node_storage:
                bind_cache[node_id] = parent_id
            else:
                parent = self.node_storage[parent_id]  # type: TipNode
                parent.AddChild(node)
                node.SetParent(parent)
        # 构建剩余结点
        for node_id, parent_id in bind_cache.items():
            if parent_id not in self.node_storage:
                raise Exception("Fail to build parent node: %s" % parent_id)
            child = self.node_storage[node_id]  # type: TipNode
            parent = self.node_storage[parent_id]  # type: TipNode
            parent.AddChild(child)
            child.SetParent(parent)
        print "[suc]", "finished build node tree: %s" % len(self.node_storage)
        # 构建绑定关系
        return True

    # 获得结点状态
    def GetNodeState(self, node_id):
        # type: (str) -> int
        """获得结点状态"""
        node = self.node_storage.get(node_id)  # type: TipNode
        if not node:
            return -1
        return node.GetState()

    # 获得结点子结点总显示
    def GetNodeChildOnNum(self, node_id):
        # type: (str) -> int
        node = self.node_storage.get(node_id)  # type: TipNode
        if not node:
            return 0
        return node.GetChildOnNum()


class TipNode(object):
    """提示结点"""
    __mVersion__ = 1

    def __init__(self, node_id):
        self.id = node_id
        self.state = 0  # 结点状态
        self.child_on_num = 0  # 子节点状态为显示的数量
        self.children = {}  # 子节点
        self.parent = None  # 父节点
        self.bind_ui = None  # 绑定ui

    # 更新结点
    def UpdateNode(self):
        """更新结点"""
        # print "[suc]", "UpdateNode:", self.id
        if not self.children:
            # 端点更新
            self.child_on_num = self.state > 0
        else:
            self.child_on_num = 0
            for node in self.children.values():
                assert isinstance(node, TipNode)
                # 更新父节点显示数量
                self.child_on_num += node.child_on_num
            self.SetState(self.child_on_num > 0, update=False)
        # 更新父节点
        if isinstance(self.parent, TipNode):
            self.parent.UpdateNode()

    # 设置状态
    def SetState(self, state, update=True):
        # type: (int, bool) -> bool
        """
        设置状态\n
        - 0: 隐藏
        - 1: 显示显示
        - 2: 显示数字
        """
        state = max(0, min(2, state))
        if state == self.state:
            return False
        self.state = state
        if update:
            self.UpdateNode()
        return True

    # 获得状态
    def GetState(self):
        # type: () -> int
        """获得状态"""
        return self.state

    # 获得子结点启用状态数量
    def GetChildOnNum(self):
        # type: () -> int
        """获得子结点启用状态数量"""
        return self.child_on_num

    # 设置父节点
    def SetParent(self, node):
        # type: (TipNode) -> None
        """设置父节点"""
        self.parent = node

    # 添加子节点
    def AddChild(self, node):
        # type: (TipNode) -> None
        """添加子节点"""
        self.children[node.id] = node


class TipUi(object):
    """提示ui"""
    __mVersion__ = 1

    def __init__(self):
        self.state = None  # 界面显示
        self.SetState(False)

    def SetState(self, state):
        if self.state == state:
            return
        self.state = state
        print "[suc]", "update", self.state


if __name__ == '__main__':
    """"""
    mgr = TipMgr()
    mgr.BuildTree({
        "root": {},
        "node1": {"parent": "root"},
        "node2": {"parent": "root"},
        "node11": {"parent": "node1"},
        "node111": {"parent": "node11"},
        "node12": {"parent": "node1"},
        "node121": {"parent": "node12"},
        "node122": {"parent": "node12"},
    })
    mgr.UpdateNode("node111", 1)
    print mgr.GetNodeChildOnNum("node1")
    mgr.UpdateNode("node121", 1)
    # mgr.UpdateNode("node122", 1)
    print mgr.GetNodeChildOnNum("node1")
